import { get } from '@/utils/request'
import { AnyAction, Reducer } from 'redux'

import { CHECK_TRADE_STATUS, PAYMENT, PLACE_AN_ORDER } from './actionNames'

export interface IOrderInfo {
  id?: number // 订单号
  amount?: number // 支付金额
  subject?: string // 订单标题
  body?: string // 订单描述
  returnUrl?: string // 支付成功后返回的页面
}

interface checkParams {
  out_trade_no: string // 待校验的订单号
}

const defaultState: IOrderInfo = {
  id: 0,
  amount: 0,
  subject: '',
}
/**
 * 提交订单
 * @param params
 * @returns
 */
export function placeAnOrder(params: any) {
  console.log(params, '$$$$$$$$$$$$$$')
  return {
    type: PLACE_AN_ORDER,
    payload: get('/api/placeanorder', params),
  }
}

/**
 * 支付接口
 */
export function payment(params: IOrderInfo) {
  return {
    type: PAYMENT,
    payload: get('/capi/alipayWap', params),
  }
}

/**
 * 检查支付情况
 */
export function checkTradeStatus(params: checkParams) {
  return get('/capi/queryOrderAlipay', params)
}

interface IAction extends AnyAction {
  type: string
  payload: {
    data: IOrderInfo
  }
}

export const orderInfo: Reducer<IOrderInfo> = (
  // 第一个代表的是state，默认类型为泛型参数一的类型
  // 第二个代表的是action，默认类型参数二的类型
  state = defaultState,
  { type, payload }
) => {
  console.log(payload, '%%%%%%%%%%%%%')
  switch (type) {
    case PLACE_AN_ORDER: {
      return { ...state, ...payload.data }
    }
  }
  return { ...state }
}

export default orderInfo
